<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="体检姓名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入体检姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
            v-model="queryParams.idCard"
            placeholder="请输入身份证号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健康证编号" prop="certificateNum" label-width="90px">
        <el-input
            v-model="queryParams.certificateNum"
            placeholder="请输入健康证编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否合格" prop="tjResult">
        <el-select v-model="queryParams.tjResult" placeholder="请选择" clearable style="width: 100px;">
          <el-option label="合格" value=1></el-option>
          <el-option label="不合格" value=0></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">刷新</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['health:certificate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['health:certificate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['health:certificate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['health:certificate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificateList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="体检姓名" align="center" prop="name" width="100"/>
      <el-table-column label="身份证号" align="center" prop="idCard" width="200"/>
<!--      <el-table-column label="头像" align="center" prop="avatar" />-->
      <el-table-column label="健康证编号" align="center" prop="certificateNum" />
      <el-table-column label="院区" align="center" prop="deptId">
        <template #default="scope">
          <span>{{ getJobTypeLabel(scope.row.deptId, hospitals) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否合格" align="center" prop="tjResult" width="100">
        <template #default="scope">
          <el-tag :type="getTjResultTagType(scope.row.tjResult)">
            {{ getTjResultLabel(scope.row.tjResult) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="体检结果说明" align="center" prop="resultDesc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['health:certificate:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['health:certificate:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改体检健康证对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="certificateRef" :model="form" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="体检姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入体检姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="健康证编号" prop="certificateNum">
          <el-input v-model="form.certificateNum" placeholder="请输入健康证编号" />
        </el-form-item>
        <el-form-item label="体检结果说明" prop="resultDesc">
          <el-input v-model="form.resultDesc" placeholder="请输入体检结果说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Certificate">
import {
  addCertificate,
  delCertificate,
  getCertificate,
  listCertificate,
  updateCertificate
} from "@/api/health/certificate/certificate.js"
import {hospitals, loadAllHospitals} from "@/api/employee/applyBaseData.js";

const { proxy } = getCurrentInstance()

const certificateList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const daterangeCreateTime = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    idCard: null,
    avatar: null,
    certificateNum: null,
    tjResult: null,
    resultDesc: null,
    deptId: null,
  },
  rules: {
    name: [
      { required: true, message: "体检姓名不能为空", trigger: "blur" }
    ],
    idCard: [
      { required: true, message: "身份证号不能为空", trigger: "blur" }
    ],
    certificateNum: [
      { required: true, message: "健康证编号不能为空", trigger: "blur" }
    ],
    tjResult: [
      { required: true, message: "是否合格不能为空", trigger: "change" }
    ],
    deptId: [
      { required: true, message: "院区不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询体检健康证列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0]
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1]
  }
  listCertificate(queryParams.value).then(response => {
    certificateList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    idCard: null,
    avatar: null,
    certificateNum: null,
    tjResult: null,
    resultDesc: null,
    deptId: null,
    createTime: null
  }
  proxy.resetForm("certificateRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加体检健康证"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCertificate(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改体检健康证"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["certificateRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCertificate(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCertificate(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除体检健康证编号为"' + _ids + '"的数据项？').then(function() {
    return delCertificate(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('health/certificate/export', {
    ...queryParams.value
  }, `certificate_${new Date().getTime()}.xlsx`)
}

// 获取体检结果标签样式
function getTjResultTagType(tjResult) {
  if (tjResult === 1) return 'success';
  if (tjResult === 2) return 'danger';
  return 'info';
}

// 获取体检结果标签
function getTjResultLabel(tjResult) {
  if (tjResult === 1) return '合格';
  if (tjResult === 2) return '不合格';
  return '-';
}

// 后台id转为中文展示方法
function getJobTypeLabel(id, list) {
  let lableName = list.find(item => item.value == id);
  return lableName ? lableName.label : '未知';
}

loadAllHospitals();
getList();
</script>
